<template>
  <div class="user-info">
    <el-card>
      <h2>用户基本信息</h2>
      <el-divider></el-divider>
      <el-form
        :model="userForm"
        label-width="80px"
        class="big-box"
        label-position="right"
      >
        <el-form-item label="账号" class="small-input" size="medium">
          <el-input v-model="userForm.name" :disabled="true"></el-input>
        </el-form-item>

        <el-form-item label="性别" class="small-input">
          <el-radio-group v-model="userForm.gender">
            <el-radio label="男"></el-radio>
            <el-radio label="女"></el-radio>
            <el-radio label="未知"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="身份" class="smallist-input">
          <el-radio-group v-model="userForm.status" disabled>
            <el-radio label="面试官"></el-radio>
            <el-radio label="面试者"></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="个人签名">
          <el-input
            v-model="userForm.intro"
            type="textarea"
            maxlength="200"
            show-word-limit
          >
          </el-input>
        </el-form-item>
      </el-form>
      <el-divider></el-divider>
      <el-button type="primary" @click="onSubmit">保存信息 </el-button>
      <el-button type="danger" @click="onLogout">退出账号 </el-button>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";
export default {
  name: "UserInfo",
  created() {
    this.getUserData();
  },
  data() {
    return {
      userForm: {
        name: "",
        gender: "",
        intro: "",
        status: "",
      },
    };
  },
  methods: {
    setInfoDefault() {
      this.userForm = {
        name: "",
        gender: "",
        intro: "",
        status: "",
      };
    },
    onLogout() {
      //登出删除用户session
      axios
        .get("/api/reqLogout/")
        .then((response) => {
          console.log(response);
          if (response.data.status == 1) {
            this.$message.warning("退出账号成功，3s后跳转到登陆界面");
            window.sessionStorage.clear();
            setTimeout(() => {
              this.$router.replace("/login");
            }, 3000);
          } else {
            this.$message.warning("用户未登录，3s后跳转到登陆界面");
            window.sessionStorage.clear();
            setTimeout(() => {
              this.$router.replace("/login");
            }, 3000);
          }
        })
        .catch((error) => {
          console.log(error);
          this.$message.error("退出账号失败");
        });
    },
    onSubmit() {
      this.updateUserinfo();
    },
    getUserData() {
      axios
        .get("/api/user")
        .then((response) => {
          console.log(response);
          this.userForm.name = response.data.result.username;
          this.userForm.intro = response.data.result.intro;
          if (response.data.result.gender == 0) {
            this.userForm.gender = "女";
          } else if (response.data.result.gender == 1) {
            this.userForm.gender = "男";
          } else if (response.data.result.gender == 2) {
            this.userForm.gender = "未知";
          }
          if (response.data.result.status == 1) {
            this.userForm.status = "面试官";
          } else if (response.data.result.status == 0) {
            this.userForm.status = "面试者";
          }
          this.$message.success("获取用户信息成功");
        })
        .catch((error) => {
          console.log(error);
          this.$message.error("获取用户信息失败");
        });
    },

    updateUserinfo() {
      this.genderExchange();
      axios
        .post("/api/user", {
          gender: this.userForm.gender,
          intro: this.userForm.intro,
        })
        .then((response) => {
          console.log(response);
          this.setInfoDefault();
          this.getUserData();
          this.$message.success("保存信息成功");
        })
        .catch((error) => {
          console.log(error);
          this.setInfoDefault();
          this.$message.error("保存信息失败");
        });
    },
    genderExchange() {
      if (this.userForm.gender == "女") {
        this.userForm.gender = 0;
      } else if (this.userForm.gender == "男") {
        this.userForm.gender = 1;
      } else {
        this.userForm.gender = 2;
      }
    },
  },
};
</script>

<style>
/* .big-box {
  border: 1px solid #dcdfe6;
  width: 800px;
  height: 400px;
  margin: 180px auto;
  padding: 10px 10px 25px 25px;
  border-radius: 5px;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  background: rgb(255, 255, 255);
  text-align: center;
  position: absolute;
  top: 20px;
  left: 700px;
  right: 10px;
}
.smallist-input {
  width: 275px;
}
.small-input {
  width: 300px;
}
.middle-input {
  width: 560px;
} */
</style>
